﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using Cricket.LinqModel;
using Cricket.LinqData;
using System.Web.Script.Services;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;

public partial class AdminCommissionReport : System.Web.UI.Page
{
    IDataRepository _dataObject = new DataRepository();
    MatchSchedule matchSchedule;
    MatchType matchType;
    List<MatchType> matchTypes;

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            HttpCookie cookie = Request.Cookies["Admin"];
            if (cookie == null)
            {
                Response.Redirect("Login.aspx");
            }
            else
            {
                bindMatch();

            }
        }
    }

    private void bindMatchType()
    {
        matchType = new MatchType(_dataObject);
        matchTypes = new List<MatchType>();
        matchTypes = matchType.GetAllMatchType();
        if (matchTypes.Count > 0)
        {
            drpMatchType.DataSource = matchTypes;
            drpMatchType.DataTextField = "Type";
            drpMatchType.DataValueField = "Id";
            drpMatchType.DataBind();
            drpMatchType.Items.Insert(0, new ListItem("Select Match Type", "0"));
        }
        else
        {
            drpMatchType.DataSource = null;
            drpMatchType.DataBind();
            drpMatchType.Items.Insert(0, new ListItem("Select Match Type", "0"));
        }
    }
    protected void drpMatchType_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (drpMatchType.SelectedValue != "")
            bindMatch();
    }

    private void bindMatch()
    {
        matchSchedule = new MatchSchedule(_dataObject);
        List<GetMatchesForReportResult> lstResult = new List<GetMatchesForReportResult>();
        lstResult = matchSchedule.GetMatchesForReport();
        if (lstResult.Count > 0)
        {
            drpMatch.DataSource = lstResult;
            drpMatch.DataTextField = "Teams";
            drpMatch.DataValueField = "Id";
            drpMatch.DataBind();
            drpMatch.Items.Insert(0, new ListItem("Select Match", "0"));
        }
    }
    protected void drpMatch_SelectedIndexChanged(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        if (drpMatch.SelectedItem.Text.ToLower() != "select match" && drpMatch.SelectedItem.Text.ToLower() != "select match type")
        {
            bindGrid(Convert.ToInt32(cookie["AdminId"]), Convert.ToInt32(drpMatch.SelectedValue));
        }
    }
    private static DataTable GetData(string query)
    {
        string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
        using (SqlConnection con = new SqlConnection(constr))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = query;
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        DataTable dt = new DataTable();
                        sda.Fill(dt);
                        return dt;
                    }
                }
            }
        }
    }

    private void bindGrid(int adminId, int matchId)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        grdBookie.Visible = true;

        DataTable dt = new DataTable();
        //dt = GetData(string.Format("select A.Id as AdminId,CA.Id, (c.FirstName+ ''+ c.LastName) as ClientName,CA.CommissionAmount AS Amount,CA.ClientId,CA.MatchId from CommissionAmount CA  Inner Join Client C ON CA.ClientId = c.Id  Inner Join Admin A ON C.AdminId = A.Id WHERE CA.AdminId = '{0}' AND CA.Matchid = '{1}'", adminId, matchId));
        dt = GetData(string.Format("Select (C.FirstName+ ' ' + C.LastName) as ClientName,SUM(CAST(CA.CommissionAmount as Decimal(18,2))) AS Amount,A.Id as AdminId ,c.Id as ClientId from CommissionAmount CA  Inner Join Client C ON CA.ClientId = C.Id  Inner Join Admin A ON C.AdminId = A.Id WHERE CA.AdminId =" + adminId + "AND CA.Matchid=" + matchId + "GROUP BY (C.FirstName + ' ' + C.LastName),A.Id,c.Id"));

        //'{0}' AND CA.Matchid = '{1}' GROUP BY (C.FirstName + ' ' + C.LastName)", adminId, matchId));
        grdBookie.DataSource = dt;
        grdBookie.DataBind();
    }
    protected void OnOrdersGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        GridView grdCustomers = (sender as GridView);
        grdCustomers.PageIndex = e.NewPageIndex;
        bindGrid(Convert.ToInt32(cookie["AdminId"]), Convert.ToInt32(drpMatch.SelectedValue));
    }
    protected void Show_Hide_ProductsGrid(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        ImageButton imgShowHide = (sender as ImageButton);
        GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
        if (imgShowHide.CommandArgument == "Show")
        {
            row.FindControl("pnlProducts").Visible = true;
            imgShowHide.CommandArgument = "Hide";
            imgShowHide.ImageUrl = "~/images/minus.png";
            int customerId = Convert.ToInt32((row.NamingContainer as GridView).DataKeys[row.RowIndex].Value);
            GridView grdAllCustomers = row.FindControl("grdAllCustomers") as GridView;
            bindGrid(Convert.ToInt32(cookie["AdminId"]), Convert.ToInt32(drpMatch.SelectedValue));
        }
        else
        {
            row.FindControl("pnlProducts").Visible = false;
            imgShowHide.CommandArgument = "Show";
            imgShowHide.ImageUrl = "~/images/plus.png";
        }
    }
    protected void Show_Hide_OrdersGrid(object sender, EventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        ImageButton imgShowHide = (sender as ImageButton);
        GridViewRow row = (imgShowHide.NamingContainer as GridViewRow);
        if (imgShowHide.CommandArgument == "Show")
        {
            row.FindControl("pnlOrders").Visible = true;
            imgShowHide.CommandArgument = "Hide";
            imgShowHide.ImageUrl = "~/images/minus.png";
            string bookieId = grdBookie.DataKeys[row.RowIndex].Value.ToString();
            GridView grdCustomers = row.FindControl("grdCustomers") as GridView;
            bindGrid(Convert.ToInt32(cookie["AdminId"]), Convert.ToInt32(drpMatch.SelectedValue));
        }
        else
        {
            row.FindControl("pnlOrders").Visible = false;
            imgShowHide.CommandArgument = "Show";
            imgShowHide.ImageUrl = "~/images/plus.png";
        }
    }
    protected void OnProductsGrid_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        HttpCookie cookie = Request.Cookies["Admin"];
        GridView grdAllCustomers = (sender as GridView);
        grdAllCustomers.PageIndex = e.NewPageIndex;
        bindGrid(Convert.ToInt32(cookie["AdminId"]), Convert.ToInt32(drpMatch.SelectedValue));
    } 
}